<?php
/**
 * Created by PhpStorm.
 * User: Argon
 * Date: 2016/12/16
 * Time: 10:21
 */

namespace Admin\Model;


use Think\Model;
use Think\Page;
class RoleModel extends Model
{
    public function getList() {
        return $this->order('sort')->select();
    }

    public function getPageResult(array $cond=[]){
        $count=$this->where($cond)->count();
        $page=new Page($count,C("PAGE.SIZE"));
        $page->setConfig("theme",C("PAGE.THEME"));
        $page_html=$page->show();
        $rows=$this->where($cond)->order('sort')->page(I("get.p"),
            C("PAGE.SIZE"))->select();
        $data=[
            'rows'=>$rows,
            'page_html'=>$page_html
        ];
        return $data;
    }

    public function addRole(){
        $this->startTrans();
        if(($roleId=$this->add())===false){
            $this->rollback();
            return false;
        }
        $permissionIds=I("post.permission_id");
        if(empty($permissionIds)){
            $this->commit();
            return true;
        }
        $data=[];
        foreach($permissionIds as $permissionId){
            $data[]=[
                'role_id'=>$roleId,
                'permission_id'=>$permissionId
            ];

        }
        if (M('RolePermission')->addAll($data) === false) {
            $this->error = '保存角色-权限关联失败';
            $this->rollback();
            return false;
        }
        $this->commit();
        return true;
    }

    public function getRoleInfo($id) {
        $row = $this->find($id);
        $row['permission_ids'] = json_encode(M('RolePermission')->where(['role_id'=>$id])->getField('permission_id',true));
        return $row;
    }

    public function remove($id){
        $this->startTrans();
        $adminRole=M("admin_role");
        if($adminRole->where("role_id=$id")->delete()===false){
            $this->rollback();
            $this->error="删除完管理员-角色联接错误";
            return false;
        }
        $rolePermission=M("role_permission");
        if($rolePermission->where("role_id=$id")->delete()===false){
            $this->rollback();
            $this->error="删除角色-权限联接错误";
            return false;
        }
        if($this->delete($id)===false){
            $this->rollback();
            $this->error="删除角色错误";
            return false;
        }

        $this->commit();
        return true;

    }


    public function saveRole($id) {
        $this->startTrans();
        //保存基本信息
        if($this->save() === false){
            $this->rollback();
            return false;
        }
        //保存权限关联
        //删除老的关联
        $role_permission_model = M('RolePermission');
        if($role_permission_model->where(['role_id'=>$id])->delete() === false){
            $this->error = '删除旧关联关系失败';
            $this->rollback();
            return false;
        }
        //添加新的关联
        $permission_ids = I('post.permission_id');
        if(empty($permission_ids)){
            $this->commit();
            return true;
        }

        $data = [];
        foreach ($permission_ids as $permission_id){
            $data[] = [
                'role_id'=>$id,
                'permission_id'=>$permission_id
            ];
        }
        if($role_permission_model->addAll($data)===false){
            $this->error = '保存关联关系失败';
            $this->rollback();
            return false;
        }
        $this->commit();
        return true;
    }
}